package ctrl;

import om.Orte;
import dao.OrteDao;
import dao.RegionDao;
import dao.RouteDao;


import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class OrteCtrl {
	private static Logger log = Logger.getLogger(OrteCtrl.class);
	private OrteDao orteDao = null;
	private RegionDao regionDao = null;

	private RouteDao routeDao = null;
	
	
	public void setRegionDao(RegionDao regionDao) {
		this.regionDao = regionDao;
	}

	public void setOrteDao(OrteDao orteDao) {
		this.orteDao = orteDao;
	}
	
	public void setRouteDao(RouteDao routeDao) {
		this.routeDao = routeDao;
	}
	
	 /**
     * <p>Orte einer Region werden aufgelistet </p>
     * <p>Erwartet HTTP GET und request '/ortevonregion.html'</p>
     */
	@RequestMapping(value="/ortevonregion.html", method=RequestMethod.GET)
	public ModelAndView getOrt(@RequestParam(required=false) Integer id) {
		log.info("orte by id");
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Auswahl des Ortes, in den Sie wandern m�chten: ");
		mv.addObject("orte", orteDao.findOrteByRegion(id));
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		mv.addObject("authorities", auth.getAuthorities());
		mv.addObject("principal", auth.getName());
		mv.setViewName("list-orte");
		return mv;
	}
	
	 /**
     * <p>Ein Ort wird angelegt </p>
     * <p>Erwartet HTTP GET und request '/EditOrt.html'</p>
     */
    @RequestMapping(value="/EditOrt.html", method=RequestMethod.GET)
    public ModelAndView 	edit(@RequestParam(required=false) Integer id){
		log.debug("EditOrt.html");
		ModelAndView mv = new ModelAndView();
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		mv.addObject("authorities", auth.getAuthorities());
		mv.addObject("principal", auth.getName());
		mv.addObject("title", "Bitte f�llen Sie alle Felder aus, sonst kann der Ort nicht angelegt werden. ");
		if (id == null){
			mv.addObject(new Orte());
		} else {
			mv.addObject(orteDao.findOrteById(id));
		}
       	mv.addObject("regionen", regionDao.getAllRegionen());
       	mv.setViewName("EditOrt");
    	return mv;
    }
    
    /**
     * <p>Ein Ort wird geoendert oder geloescht </p>
     * <p>Erwartet HTTP GET und request '/delete-edit-orte.html'</p>
     */

    @RequestMapping(value="/delete-edit-orte.html", method=RequestMethod.GET)
    public ModelAndView getAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Alle Orte der Webseite:");
				
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		mv.addObject("authorities", auth.getAuthorities());
		mv.addObject("principal", auth.getName());
		mv.addObject("orte", orteDao.getAllOrte());	
		mv.addObject("edit", true);
		mv.setViewName("list-all-orte");
		return mv;
    }
    

    /**
     * <p>Loescht einen Ort,die dazugehoerigen Bewertungen und die Bilder.</p>
     * 
     * <p>Erwartet HTTP GET und request '/LoescheRoute.html'.</p>
     */

    @RequestMapping(value="/LoescheOrt.html", method=RequestMethod.GET)
    public ModelAndView delete(@RequestParam(required=true)Integer id) {
        try {
        	
        	orteDao.delete(orteDao.findOrteById(id), routeDao.findRouteByOrt(id));
	        return new ModelAndView("redirect:delete-edit-orte.html");
	       
        }
			
		 catch (dao.DaoException e) {
			ModelAndView mv = new ModelAndView("error");
			mv.addObject("message", e.getMessage());
			return mv;
		
		 }
    }

    /**
     * <p>Speichert einen Ort</p>
     * 
     * <p>Erwartet HTTP POST und request '/SaveOrt.html'</p>
     * @return 
     */
    @RequestMapping(value="/SaveOrt.html", method=RequestMethod.POST)
    public String save(Orte orte, Integer regionid) {
        Orte result = orteDao.save(orte, regionid);
        return "redirect:EditRoute.html";
    }
}
